<?php

/**
 * Created by PhpStorm.
 * User: Brian
 * Date: 07/08/14
 * Time: 14:27
 */
class Branch extends Users
{

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Branch the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, nickname,firstname, lastname, gender, occupation, address, address2, city, country, postal_code, type, status, business_id, business_name, business_type, business_logo, business_description, business_slogan, tax_id, website, email, author_id, date_created, facebook, twitter, gplus, instagram, latitude , longitude', 'safe', 'on' => 'search'),
        );
    }

    /**
     * Return edit url
     * @return bool|string
     */
    public function getEditUrl()
    {
        return url('branch/edit', array('pk' => $this->ukey));
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.
        $filterBusiness = null;
        $criteria       = new CDbCriteria;
        $criteria->compare('type', Merchant::TYPE_BUSINESS);
        if (user()->isMerchant())
        {
//            $filterBusiness = Users::getUkeyById(Yii::app()->user->id);

            $criteria->compare('author_id', Yii::app()->user->id);
        }
        elseif (user()->isAdmin())
        {
            if (isset($_GET['pk']))
            {
                $filterBusiness = $_GET['pk'];
            }
            else
            {
                $bizArray = Users::getAllBussiness(TRUE);
                foreach ($bizArray as $bizId => $bizName)
                {
                    $filterBusiness = $bizId;
                    break;
                }
            }

            $criteria->compare('author_id', Users::getIdByUkey($filterBusiness));
        }
        else
            $criteria->addCondition('1=0');
        if (($email = Yii::app()->request->getQuery('email', '')) != '')
        {
            $criteria->compare('email', $email, TRUE);
        }
        if (($bname = Yii::app()->request->getQuery('bname', '')) != '')
        {
            $criteria->compare('business_name', $bname, TRUE);
        }
        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

}
